//
//  DrawView.m
//  testIbeacons1
//
//  Created by SeaSea_Leung on 14-1-22.
//  Copyright (c) 2014年 SeaSea_Leung. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView

//每次触摸结束前经过的点，形成线的点数组
static NSMutableArray *pointArray;
static NSMutableArray *lineArray;
static NSMutableArray *deleArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        pointArray=[[NSMutableArray alloc]init];
        lineArray=[[NSMutableArray alloc]init];
        deleArray=[[NSMutableArray alloc]init];
    }
    return self;
}

//重写方法
- (void)drawRect:(CGRect)rect
{
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 5.0f);
    CGContextSetLineJoin(context,kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    if ([pointArray count]>0)
    {
        //CGContextBeginPath(context);
        CGPoint myStartPoint=CGPointFromString([pointArray objectAtIndex:0]);
        CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
        for (int j=0; j<[pointArray count]-1; j++)
        {
            CGPoint myEndPoint=CGPointFromString([pointArray objectAtIndex:j+1]);
            CGContextAddLineToPoint(context, myEndPoint.x,myEndPoint.y);

        }
        //设置颜色
        UIColor *lineColor=[UIColor redColor];
        CGContextSetStrokeColorWithColor(context,[lineColor CGColor]);
        CGContextStrokePath(context);
    }
    
    
}


/*-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
      NSLog(@"touch move");
     UITouch* touch=[touches anyObject];
     MyBeganpoint=[touch locationInView:self];
     NSString *sPoint=NSStringFromCGPoint(MyBeganpoint);
     [pointArray addObject:sPoint];
     [self setNeedsDisplay];

}*/

-(void)moveBegan:(CGPoint)aPoint
{
    NSString *sPoint =NSStringFromCGPoint(aPoint);
    [pointArray addObject:sPoint];
    [self setNeedsDisplay];
}
-(void)clear{
    //移除所有信息
    [deleArray removeAllObjects];
    [lineArray removeAllObjects];
    [pointArray removeAllObjects];
    [self setNeedsDisplay];
   }

@end
